package com.b2b.controller;


import com.alibaba.fastjson.JSONObject;
import com.b2b.model.CollectionDto;
import com.b2b.model.GoodsDto;
import com.b2b.model.OrdersListDto;
import com.b2b.model.StockDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.model.entity.goods.Collection;
import com.b2b.model.entity.goods.Goods;
import com.b2b.model.entity.goods.Stock;
import com.b2b.service.goodsService.CollectionService;
import com.b2b.service.goodsService.GoodsService;
import com.b2b.service.goodsService.StockService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/collection")
public class CollectionController {

    @Autowired
    CollectionService collectionService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    StockService stockService;

    @PostMapping("/add")
    public ResponseBean add(@RequestBody Collection collection){
        if(collectionService.selectCount(collection) == 0){
            collectionService.insert(collection);
        }else {
            return new ResponseBean(HttpStatus.OK.value(), "已在收藏", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "收藏成功", null);
    }

    @DeleteMapping("/delete")
    public ResponseBean delete(@RequestParam Map<String, Object> map){
        Integer companyId = Integer.parseInt(map.get("companyId").toString());
        Integer goodsId = Integer.parseInt(map.get("goodsId").toString());
        CollectionDto collectionDto = new CollectionDto();
        collectionDto.setGoodsId(goodsId);
        collectionDto.setCompanyId(companyId);
        if (collectionService.selectCount(collectionDto) != 0){
            collectionService.delete(collectionDto);
            return new ResponseBean(HttpStatus.OK.value(), "删除成功", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "要删除的记录不存在", null);
    }

    @GetMapping("/getMy")
    public ResponseBean getMy(@RequestParam Map<String, Object> map){
        int page = 1, rows = 10;
        if(map.containsKey("page"))
            page = Integer.parseInt(map.get("page").toString());
        if(map.containsKey("rows"))
            rows = Integer.parseInt(map.get("rows").toString());
        PageHelper.startPage(page, rows);
        Integer companyId = Integer.parseInt(map.get("companyId").toString());
        Collection collection = new Collection();
        collection.setCompanyId(companyId);
        List<Collection> collectionList = collectionService.select(collection);
        List<CollectionDto> collectionDtoList = new ArrayList<>();
        for (Collection c:collectionList
             ) {
            CollectionDto collectionDto = new CollectionDto();
            Goods goods = goodsService.selectByPrimaryKey(c.getGoodsId());
            collectionDto.setGoodsId(c.getGoodsId());
            collectionDto.setCompanyId(c.getCompanyId());
            collectionDto.setGoodsName(goods.getGoodsName());
            collectionDto.setGoodsImgUrl(JSONObject.parseArray(goods.getGoodsImgUrlList()).getString(0));
            StockDto stockDto = new StockDto();
            stockDto.setGoodsId(goods.getGoodsId());
            List<Stock> stockList = stockService.select(stockDto);
            double min = Integer.MAX_VALUE;
            for (Stock s:stockList
                 ) {
                if(s.getPrice() < min)
                    min = s.getPrice();
            }
            collectionDto.setPrice(min);
            collectionDtoList.add(collectionDto);
        }

        PageInfo<CollectionDto> selectPage = new PageInfo<>(collectionDtoList);
        Map<String, Object> result = new HashMap<>();
        result.put("count", selectPage.getTotal());
        result.put("collectionList", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }
}
